package JavaUtils.web.req.io;

import JavaUtils.CSUtils.CFile;
import JavaUtils.CSUtils.CString;
import JavaUtils.web.IWebCallback;
import JavaUtils.web.ReqInfo;

import java.io.IOException;

public class Req_fileRead {
    public static IWebCallback ReadAllTxt() {
        IWebCallback readAllTxtEvent = new IWebCallback() {
            @Override
            public void MsgHandle(ReqInfo reqInfo) throws IOException {
                String filePath = reqInfo.dataMap.get("filePath");
                if (CString.IsNullOrEmpty(filePath)) {
                    reqInfo.resp_txt = "0";
                }
                String fileData = CFile.ReadAllTxt(filePath);
                reqInfo.resp_txt = fileData;
            }
        };
        return readAllTxtEvent;
    }

    public static IWebCallback ReadAllBytes() {
        IWebCallback readAllTxtEvent = new IWebCallback() {
            @Override
            public void MsgHandle(ReqInfo reqInfo) throws IOException {
                String filePath = reqInfo.dataMap.get("filePath");
                if (CString.IsNullOrEmpty(filePath)) {
                    reqInfo.resp_txt = "0";
                }
                byte[] fileData = CFile.ReadAllBytes(filePath);
                reqInfo.resp_bytes = fileData;
            }
        };
        return readAllTxtEvent;
    }
}
